********************************************************************************
*** Balance Tables
use "data/pseudodata/userlevel_pseudodata.dta", clear

local vars = "totalagentpremium dayrate makemodelyearvalue_private car_year age income_insight_score vantage_v4_score totinq totalrevlimit cardlimit cardbalance isconstrained autoloan autoloanamount medcoll nonmedcoll"

label variable isconstrained "Is Credit Constrained"
label variable totalrevlimit "Total Revolving Credit Limit"
label variable car_year "Vehicle Year"
label variable vantage_v4_score "Vantage Credit Score"
label variable makemodelyearvalue_private "Vehicle Resale Value \hangindent=1em"
label variable totalagentpremium "3-Month Premium"
label variable dayrate "Daily Premium"

label variable clar_totinq "Clarity Total Inquiries"
label variable clar_limits "Clarity Credit Limit"
label variable clar_balance "Clarity Credit Balance"

local pcts = "isconstrained autoloan"
foreach pct of local pcts {
	replace `pct' = `pct'*100
}

replace income_insight_score = income_insight_score*1000

********************************************************************************
*** Balance Tables
preserve

	gen dailystr = "Pay-As-You-Go" if daily == 1
	replace dailystr = "Traditional" if daily == 0
	encode dailystr, gen(dailystr2)

	encode price, gen(pricestr)

	gen bundlest = "Bundle Discount" if bundle == 1
	replace bundlest = "No Bundle Discount" if bundle == 0 
	encode bundlest, gen(bundlestr)

	*** daily v non-daily
	eststo clear
	forval i=1/5 {
		if inlist(`i', 1,2) local j=1
		if inlist(`i', 3,4) local j=0
		if inlist(`i', 1,2,3,4) eststo est`i': estpost tabstat `vars' if daily==`j', ///
			statistics(mean sd n) columns(statistics)
		if inlist(`i', 5) eststo est`i': estpost ttest `vars', by(daily)
		if inlist(`i', 1,3) matrix main = e(mean)
		if inlist(`i', 1,3) matrix aux = e(sd)
		if inlist(`i', 5) matrix main = e(b)
		if inlist(`i', 5) matrix aux = e(se)
		if inlist(`i', 2,4) matrix main = e(count)
		if inlist(`i', 1,3,5) local arrs = "main aux"
		if inlist(`i', 2,4) local arrs = "main"
		foreach arr of local arrs {
			local cols = colsof(`arr')
			forval j=1/`cols' {
				mat `arr'[1,`j'] =  round(`arr'[1,`j'], ///
					10^(min(-2, int(log10(abs(`arr'[1,`i'])))-2)))
			}
			estadd matrix `arr' = `arr'
			mat list `arr'
		}
	}
	reg daily `vars', r
	ereturn list
	local fstat : di %12.3gc `e(F)'
	local pval : di %12.3f Ftail(`e(df_m)',`e(df_r)',`e(F)')
	
	esttab est1 est2 est3 est4 est5 ///
		using "output/tables/balance table by daily.tex", ///
		label nonumber replace noobs nogaps ///
		cells(main(fmt(%12.3gc) label(\vspace{-.5ex}\phantom{))  ///
			aux(par(( )\vspace{.2ex}) label(}))) ///
		mlabels("Mean/SD" "N" "Mean/SD" "N" "Diff/SE" "Joint Test") ///
		mgroups("\vspace{-1ex}Pay-As-You-Go" "Traditional", ///
			pattern(1 0 1 0 1) ///
			prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
			varwidth(15) wrap ///
			prefoot("\hline Omnibus F-Statistic & & & & & `fstat' \\ \hspace{1em}p-value & & & & & `pval' \\")
				
restore

/********************************************************************************
*** Comparison to representative samples
use "data/cleaned/hugo and expclar.dta", clear

gen in_expt = 1 if !missing(cohort)
keep if in_expt == 1 | ussamp == 1 | casamp == 1

label variable isconstrained "No Available Credit"
label variable totalrevlimit "Total Revolving Credit Limit"
replace balcred = 100 if balcred == 101

label variable in_expt "Hugo Sample"

local vars = "income_insight_score vantage_v4_score totinq totalrevlimit cardlimit cardbalance isconstrained autoloan autoloanamount medcoll nonmedcoll clar_opentrades clar_deltrades clar_limits clar_balance clar_util clar_totinq"

eststo clear

gen samp = .
forval i=1/6 {
	if inlist(`i', 1,2) replace samp = in_expt==1
	if inlist(`i', 3,4) replace samp = casamp==1
	if inlist(`i', 5,6) replace samp = ussamp==1
	eststo est`i': estpost tabstat `vars' if samp == 1, ///
		statistics(mean sd n count) columns(statistics)
	if inlist(`i', 1,3,5) matrix main = e(mean)
	if inlist(`i', 1,3,5) matrix aux = e(sd)
	if inlist(`i', 1,3,5) local arrs = "main aux"
	if !inlist(`i', 1,3,5) matrix main = e(count)
	if !inlist(`i', 1,3,5) local arrs = "main"
	foreach arr of local arrs {
			local cols = colsof(`arr')
			forval j=1/`cols' {
				mat `arr'[1,`j'] =  round(`arr'[1,`j'], 10^(min(-2, int(log10(abs(`arr'[1,`j'])))-2)))
			}
			estadd matrix `arr' = `arr'
	}
}

esttab est1 est2 est3 est4 est5 est6 ///
	using "output/tables/credit variables by sample.tex", ///
	label nonumber replace noobs nogaps ///
	cells("main(fmt(%12.3gc) label(\vspace{-.5ex}\phantom{))" ///
		aux(par(( )\vspace{.2ex}) label(}))) ///
	varwidth(25) wrap ///
	mlabels("Mean/SD" "N" "Mean/SD" "N" "Mean/SD" "N") ///
	mgroups("\vspace{-1ex}Hugo" "CA Sample" "US Sample", ///
		pattern(1 0 1 0 1 0) ///
		prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))		

eststo clear

local evars = "income_insight_score vantage_v4_score totinq totalrevlimit cardlimit cardbalance isconstrained autoloan autoloanamount medcoll nonmedcoll"
local cvars = "clar_totinq clar_limits clar_balance"

cap drop samp
gen samp = .
forval k=1/2 {
	forval i=1/6 {
		if inlist(`i', 1,2) replace samp = in_expt==1 & !mi(vantage_v4_score)
		if inlist(`i', 3,4) replace samp = casamp==1
		if inlist(`i', 5,6) replace samp = ussamp==1
		if `k'==1 local vars = "`evars'"
		if `k'==2 local vars = "`cvars'"
		eststo est`k'_`i': estpost tabstat `vars' if samp == 1, ///
			statistics(mean sd n count) columns(statistics)
		if inlist(`i', 1,3,5) matrix main = e(mean)
		if inlist(`i', 1,3,5) matrix aux = e(sd)
		if inlist(`i', 1,3,5) local arrs = "main aux"
		if !inlist(`i', 1,3,5) matrix main = e(count)
		if !inlist(`i', 1,3,5) local arrs = "main"
		foreach arr of local arrs {
				local cols = colsof(`arr')
				forval j=1/`cols' {
					mat `arr'[1,`j'] =  round(`arr'[1,`j'], 10^(min(-2, int(log10(abs(`arr'[1,`j'])))-2)))
				}
				estadd matrix `arr' = `arr'
		}
		if inlist(`i', 2,4,6) & `k'==1 {
			qui summ totinq if !mi(vantage_v4_score) & samp==1
			estadd scalar n = `r(N)'
		}
		if inlist(`i', 2,4,6) & `k'==2 {
			qui summ clar_totinq if clar_totinq!=0 & samp==1
			estadd scalar n = `r(N)'
		}
	}
}

esttab est1_1 est1_2 est1_3 est1_4 est1_5 est1_6 ///
	using "output/tables/credit variables by sample.tex", ///
	prehead("\begin{tabular}{l*{6}{c}} \hline\hline") ///
	posthead("\hline \\ \multicolumn{7}{c}{\textbf{Panel A: Experian}} \\\\[-1ex]") ///
	prefoot("\hline") postfoot("\hline \vspace{1em}") ///
	fragment ///
	label nonumber replace noobs nogaps ///
	cells("main(fmt(%12.3gc) label(\vspace{-.5ex}\phantom{))" ///
		aux(par(( )\vspace{.2ex}) label(}))) ///
	stats(n, fmt(%11.3gc) labels("Reports")) ///
	varwidth(25) wrap ///
	mlabels("Mean/SD" "N" "Mean/SD" "N" "Mean/SD" "N") ///
	mgroups("\vspace{-1ex}Hugo" "CA Sample" "US Sample", ///
		pattern(1 0 1 0 1 0) ///
		prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		
esttab est2_1 est2_2 est2_3 est2_4 est2_5 est2_6 ///
	using "output/tables/credit variables by sample.tex", ///
	posthead(" \multicolumn{7}{c}{\textbf{Panel B: Clarity}} \\\\[-1ex]") ///
	fragment append ///
	nomtitles nonumbers nolines ///
	prefoot("\hline") postfoot("\hline\hline \end{tabular}") ///
	label unstack ///
	cells("main(fmt(%12.3gc) label(\vspace{-.5ex}\phantom{))" ///
		aux(par(( )\vspace{.2ex}) label(}))) ///
	stats(n, fmt(%11.3gc) labels("Reports")) ///
	varwidth(25) wrap
*/




